"""
读取方法
- read
- for-in
- readlines

Version: 0.1
Author: yuwl
Date：2024-4-07
"""

import time


def main():
    # 一次性读取整个文件内容
    with open('Answer.txt', 'r', encoding='utf-8') as f:
        print(f.read())

    # 通过 for-in 循环逐行读取
    with open('Answer.txt', mode='r', encoding='utf-8') as f:
        for line in f:
            print(line, end='')
            time.sleep(0.5)
    print()

    # 读取文件内容到列表中
    with open('Answer.txt', 'r', encoding='utf-8') as f:
        lines = f.readlines()
    print(lines)

    # 按固定大小读取
    chunk_size = 1024 # 1KB
    with open('Answer.txt', 'r', encoding='utf-8') as f:
        while True:
            chunk = f.read(chunk_size)
            if not chunk:
                break
            print(chunk, end='')
    print()

if __name__ == '__main__':
    main()